<?php
namespace app\live\controller\sys_admin;

use app\shop\model\GoodsModel;
use think\Db;

use app\AdminController;
use app\live\model\LiveGoodsModel;
/**
 * 直播商品
 * Class Index
 * @package app\store\controller
 */
class LiveGoods extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new LiveGoodsModel();
    }

    /*------------------------------------------------------ */
    //--首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
        $keyword = input('keyword','','trim');
        if (empty($keyword) == false){
            $where[] = ['name','like','%'.$keyword.'%'];
        }
        $data = $this->getPageList($this->Model,$where);
        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //--上传商品
    /*------------------------------------------------------ */
    public function add()
    {
        if ($this->request->isPost()) {
            $goods_id = input('goods_id',0,'intval');
            if ($goods_id < 1){
                return $this->error('请选择商品.');
            }
            $count = $this->Model->where('goods_id', $goods_id)->count();
            if ($count > 0){
                return $this->error('当前商品已上传过，请不要重复提交.');
            }
            $goods_img = input('goods_img','','trim');
            if (empty($goods_img)){
                return $this->error('请选择商品图片.');
            }
            $postData['goods_name'] = input('goods_name','','trim');
            if (empty($postData['goods_name'])){
                return $this->error('请输入商品名称.');
            }
            $postData['is_cart'] = input('is_cart',1,'intval');
            $postData['price_type'] = input('price_type',1,'intval');
            $postData['price'] = input('price',0,'intval');
            $postData['price2'] = input('price2',0,'intval');
            $postData['goods_num'] = input('goods_num',0,'intval');
            if (empty($postData['price'])){
                return $this->error('price字段必须填写.');
            }
            if ($postData['price_type'] > 1 && empty($postData['price2'])){
                return $this->error('请填写price2字段.');
            }
            if ($postData['goods_num'] < 0){
                return $this->error('库存不能为空.');
            }

            $goods = (new GoodsModel())->info($goods_id);
            if ($postData['goods_num'] > $goods['goods_number']){
                return $this->error('库存不可超出商城同一商品库存');
            }

            $postData['url'] = config('config.xcx_goods_path').$goods_id;

            $inArr = $postData;
            $inArr['goods_id'] = $goods_id;
            $inArr['goods_img'] = $goods_img;
            $inArr['add_time'] = time();
            $res = $this->Model->save($inArr);
            if ($res == false){
                return $this->error('写入数据库失败.');
            }
            $this->_log($res->id,'添加直播商品');
            return $this->success('添加成功.');
        }
        return $this->fetch('add');
    }

    public function beforeEdit($row){
        $goods = (new GoodsModel())->info($row['goods_id']);
        $liveGoods = $this->Model->where('goods_id', $row['goods_id'])->find();
        if ($liveGoods['goods_num'] + ($row['goods_num'] * 1) > $goods['goods_number']){
            return $this->error('库存不可超出商城同一商品库存');
        }
        $row['goods_num'] = ['INC', $row['goods_num'] * 1];
        return $row;
    }

    /*------------------------------------------------------ */
    //--商品删除操作
    /*------------------------------------------------------ */
    public function del()
    {
        $id = input('id',0,'intval');
        if ($id < 1){
            return $this->error('ID传参出错.');
        }
        $row = $this->Model->where('id',$id)->find();
        if (empty($row)){
            return $this->error('没有找到相关数据.');
        }
        $res = $this->Model->where('id',$id)->delete();
        if ($res < 1){
            return $this->error('删除失败.');
        }
        $this->_log($id,'删除直播商品');
        return $this->success('删除成功.',url('index'));
    }
    /*------------------------------------------------------ */
    //--选择商品
    /*------------------------------------------------------ */
    public function select()
    {
        $list = $this->Model->select()->toArray();
        $this->assign('list',$list);
        $this->assign('price_type',$this->Model->priceType);
        return $this->fetch('select');
    }

    public function explain() {
        $live_goods = input('id',0,'intval');
        if ($live_goods < 1){
            return $this->error('传参错误.');
        }
        $status = input('status',0,'intval');
        Db::startTrans();
        if ($status==1) {
            $this->Model->where(['is_explain'=>1])->update(['is_explain'=>0]);
        }
        $row = $this->Model->where('id',$live_goods)->update(['is_explain'=>$status]);
        if ($row) {
            Db::commit();
            return $this->success('操作成功.');
        } else {
            Db::rollback();
            return $this->success('操作失败.');
        }
    }
}
